package top.chengdongqing.mishop.cart

import org.springframework.http.HttpStatus
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/shopping-cart")
class CartController(private val cartService: CartService) {

    @GetMapping
    fun findCartItems(@AuthenticationPrincipal userId: Int): List<CartItemDTO> {
        return cartService.findCartItems(userId)
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    fun addToCart(@AuthenticationPrincipal userId: Int, @RequestBody item: CartItemDTO): Int {
        return cartService.addToCart(userId, item)
    }

    @PutMapping
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun modifyCartItems(@AuthenticationPrincipal userId: Int, @RequestBody items: List<CartItemDTO>) {
        cartService.modifyCartItems(userId, items)
    }

    @DeleteMapping
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun removeCartItems(@AuthenticationPrincipal userId: Int, @RequestBody ids: List<Int>) {
        cartService.removeCartItems(userId, ids)
    }

    @PostMapping("/sync")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun syncCart(@AuthenticationPrincipal userId: Int, @RequestBody items: List<CartItemDTO>) {
        cartService.syncCart(userId, items)
    }
}